package com.fauks.ssm.springbyanno;

import com.fauks.ssm.springbyanno.bean.Cat;
import com.fauks.ssm.springbyanno.bean.Person;
import com.fauks.ssm.springbyanno.config.IOCConfig;
import com.fauks.ssm.springbyanno.config.ProfileConfig;
import com.fauks.ssm.springbyanno.service.CatService;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.Environment;

import java.util.Map;

/**
 * 组件注册
 */
public class _01TestComponent {

    private AnnotationConfigApplicationContext ac;

    @Before
    public void initContext() {
        ac = new AnnotationConfigApplicationContext();
    }

    @Test
    public void test1() {
        ApplicationContext ac = new AnnotationConfigApplicationContext(IOCConfig.class);
        Cat cat = ac.getBean("cat", Cat.class);
        System.out.println(cat);

        //所有的属性配置文件都会打包到Property中
        Environment environment = ac.getEnvironment();
        String cat_address = environment.getProperty("cat_address");
        System.out.println(cat_address);
    }

    @Test
    public void testValue() {
        ApplicationContext ac = new AnnotationConfigApplicationContext(ProfileConfig.class);

    }

    @Test
    public void testBeans() {
        ApplicationContext ac = new AnnotationConfigApplicationContext(IOCConfig.class);
        //获取左右定义的bean的名称
        printBeanNames(ac);

        CatService catService = ac.getBean("catService", CatService.class);
        System.out.println(catService);
    }


    @Test
    public void test2() {
        ApplicationContext ac = new AnnotationConfigApplicationContext(IOCConfig.class);

        Map<String, Cat> beansOfType = ac.getBeansOfType(Cat.class);
        System.out.println(beansOfType);
       /* String[] beanNamesForType = ac.getBeanNamesForType(Cat.class);
        for (String s : beanNamesForType) {
            System.out.println(s);
        }*/
    }

    @Test
    public void testFactoryBean() {
        Person person = (Person) ac.getBean("personFactoryBean");
        person.sayHello();
    }

    /**
     * 测试Bean声明周期
     */
    @Test
    public void testBeanLifeCycle() {
        //在容器启动时会构造Bean并调用Bean的init方法
        //在容器关闭时，会调用Bean的destory方法
        ac.close();
    }

    @Test
    public void testProfile() {
        //设置需要激活的环境
        ac.getEnvironment().setActiveProfiles("dev", "release");
        ac.register(ProfileConfig.class);
        ac.refresh();
        String[] beanDefinitionNames = ac.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            System.out.println(beanDefinitionName);
        }
    }

    private void printBeanNames(ApplicationContext context) {
        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            System.out.println(beanDefinitionName);
        }
    }
}
